﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using System.Windows.Input;

namespace BlogTruyen
{
    public partial class TopBar : UserControl
    {
        //public delegate void ExecuteSearchEventHandler(object sender, EventArgs e);
        public event EventHandler ExecuteSearch;
        public event EventHandler MenuIconTap;
        public string Title
        {
            get
            {
                return tbTitle.Text;
            }
            set
            {
                tbTitle.Text = value.ToUpper();
            }
        }
        public TopBar()
        {
            InitializeComponent();
        }
        private void tbSearch_ActionIconTapped(object sender, EventArgs e)
        {
            if(ExecuteSearch!=null)
                ExecuteSearch(sender,e);
        }
        private void tbSearch_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                tbSearch_ActionIconTapped(sender, e);
            }
        }
        private void tbSearch_LostFocus(object sender, RoutedEventArgs e)
        {
            if (tbSearch.Text.Length == 0)
            {
                spTitle.Visibility = Visibility.Visible;
            }
        }
        private void imgSearch_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            spTitle.Visibility = Visibility.Collapsed;
            tbSearch.Focus();
        }
        private void tbSearch_GotFocus(object sender, RoutedEventArgs e)
        {
            tbSearch.Background = 0xffffffff.ToSolidColorBrush();
            tbSearch.BorderBrush = 0xff000000.ToSolidColorBrush();
        }

        public string SearchQuery { 
            get {
                return tbSearch.Text;
            }
            set
            {
                tbSearch.Text = value;
                if (value != null && value.Length > 0)
                {
                    spTitle.Visibility = Visibility.Collapsed;
                }
            }
        }

        private void Image_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            if (MenuIconTap != null)
                MenuIconTap(sender, e);
        }
    }
}
